#include <iostream>
#include <string>
using namespace std;

//普通方法 ：
class Calculator {
public:
    int m_num1 , m_num2;
    int getResult(string oper) {
        if (oper == "+") {
            return m_num1 + m_num2;
        }
        else if (oper == "-") {
            return m_num1 - m_num2;
        }
        else if (oper == "*") {
            return m_num1 * m_num2;
        }
        else if (oper == "/") {
            return m_num1 / m_num2;
        }
        else {
            cout << "输入运算符有误" << endl;
            return 0;
        }
    }
};

void test01() {
    Calculator c;
    c.m_num1 = 10;
    c.m_num2 = 20;

    cout << c.getResult("+") << endl;
    cout << c.getResult("-") << endl;
    cout << c.getResult("*") << endl;
    cout << c.getResult("/") << endl;
    cout << c.getResult("a") << endl;
}

// (抽象类 ：abstract ) 利用多态实现计算器
class AbstractCalculator {
public:
    int m_num1 , m_num2;
    virtual int getResult() {
        return 0;
    }

    //抽象类：

    //virtual int getResult() = 0;
};

//加法器类
class AddCalculator : public AbstractCalculator {
public:
    virtual int getResult() {
        return m_num1 + m_num2;
    }
};
//减法器类
class SubCalculator : public AbstractCalculator {
public:
    virtual int getResult() {
        return m_num1 - m_num2;
    }
};
//乘法器类
class MulCalculator : public AbstractCalculator {
public:
    virtual int getResult() {
        return m_num1 * m_num2;
    }
};
//除法器类
class DivCalculator : public AbstractCalculator {
public:
    virtual int getResult() {
        return m_num1 / m_num2;
    }
};
int getresult(AbstractCalculator & calculator , int num1 , int num2) {
    calculator.m_num1 = num1;
    calculator.m_num2 = num2;
    return calculator.getResult();
}
void test02() {
    //加法
    AbstractCalculator * ret = new AddCalculator;  //多态的调用 ： 父类的指针或引用指向子类对象
    ret -> m_num1 = 40;
    ret -> m_num2 = 20;
    cout << ret -> getResult() << endl;
    delete ret;

    //减法
    ret = new SubCalculator;
    ret -> m_num1 = 40;
    ret -> m_num2 = 20;
    cout << ret -> getResult() << endl;
    delete ret;

    //乘法
    ret = new MulCalculator;
    ret -> m_num1 = 40;
    ret -> m_num2 = 20;
    cout << ret -> getResult() << endl;
    delete ret;

    //除法
    ret = new DivCalculator;
    ret -> m_num1 = 40;
    ret -> m_num2 = 20;
    cout << ret -> getResult() << endl;
    delete ret;
}
int main() {
    cout << "-------------多态案例 1_计算器类-------------------" << endl;
    test01();
    cout << "-------------多态案例 2_抽象类---------------------" << endl;
    test02();
    return 0;
}
